clear all

#delimit ;
*  PSID DATA CENTER *****************************************************
   JOBID            : 296132                            
   DATA_DOMAIN      : IND                               
   USER_WHERE       : NULL                              
   FILE_TYPE        : All Individuals Data              
   OUTPUT_DATA_TYPE : ASCII                             
   STATEMENTS       : do                                
   CODEBOOK_TYPE    : PDF                               
   N_OF_VARIABLES   : 9                                 
   N_OF_OBSERVATIONS: 18230                             
   MAX_REC_LENGTH   : 15                                
   DATE & TIME      : August 3, 2021 @ 12:48:18
*************************************************************************
;

infix
      ER30000              1 - 1           ER30001              2 - 5           ER30002              6 - 8     
      ER32022              9 - 10          V1                  11 - 11          V122                12 - 12    
      V123                13 - 13          V398                14 - 14          ER30003             15 - 15    
using ./RawData/number_children.txt, clear 
;
label variable ER30000         "RELEASE NUMBER"                           ;
label variable ER30001         "1968 INTERVIEW NUMBER"                    ;
label variable ER30002         "PERSON NUMBER                         68" ;
label variable ER32022         "# LIVE BIRTHS TO THIS INDIVIDUAL"         ;
label variable V1              "RELEASE NUMBER"                           ;
label variable V122            "NR CHLDRN LVNG AWAY"                      ;
label variable V123            "NR CHLDRN IN INSTIT"                      ;
label variable V398            "NUMBER OF CHILDREN"                       ;
label variable ER30003         "RELATIONSHIP TO HEAD                  68" ;


do ./RawData/number_children_formats.do ;
#delimit cr

	rename ER30003 relationship_head
	gen head = relationship_head==1
	
	rename ER32022 number_children_all
	
	rename V122 number_children_away_1968
	replace number_children_away_1968=. if number_children_away_1968==9
	
	rename V123 number_children_inst_1968
	replace number_children_inst_1968=. if number_children_inst_1968==9
	
	rename V398 number_children_FU_1968
	replace number_children_FU_1968=. if number_children_FU_1968==9
	
	/* Create unique identifer, keep pertinent variables 
	   and save */
	ren ER30001 famid
	ren ER30002 personnumber

	gen father_id = famid*1000 + personnumber
	label var father_id "Child 1968 ID"
	duplicates report father_id //no duplicates
	
	keep father_id famid personnumber number_children* head
		
	sort father_id 
	compress
	save ./RawData/number_children_info, replace
